package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class RenewalleaseApplySendMessage extends ExecuteListener{
	
	public String getDescription() {
		return "办理续租审批结束，给续租人发企业微信消息及更新入住信息。";
	}

	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		// TODO Auto-generated method stub
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_SH_RENEWALLEASE  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String FID=CoreUtil.objToStr(jcbd.get("FID"));//租户表id
			String APPLYNAMEID=CoreUtil.objToStr(jcbd.get("APPLYNAMEID"));//租户账号
			String RENTBEGINTIME=CoreUtil.objToStr(jcbd.get("RENTBEGINTIME"));//租用开始时间
			String RENTENDTIME=CoreUtil.objToStr(jcbd.get("RENTENDTIME"));//租用结束时间
			String ANNEX=CoreUtil.objToStr(jcbd.get("ANNEX"));//租赁合同附件
			String APPLYUNIT=CoreUtil.objToStr(jcbd.get("APPLYUNIT"));//申请单位
			String APPLYNAME=CoreUtil.objToStr(jcbd.get("APPLYNAME"));//申请人
			String APPLYTELEPHONE=CoreUtil.objToStr(jcbd.get("APPLYTELEPHONE"));//申请人电话
			String ID=CoreUtil.objToStr(jcbd.get("ID"));//ID
			//删除以前租赁合同附件
			String sqlfujian ="select ANNEX  from BO_EU_SH_RCGY_ROOM_PSN  where ID='"+FID+"'";
	        String oldannex =CoreUtil.objToStr(DBSql.getString(sqlfujian,"ANNEX"));
	        if(!"".equals(oldannex)) {
	        	SDK.getBOAPI().removeFiles(FID, oldannex);
	        }
			//更新入住信息
			BO newcmsrecord = new BO();
			newcmsrecord.set("RENTBEGINTIME",RENTBEGINTIME);//租用开始时间
			newcmsrecord.set("RENTENDTIME",RENTENDTIME);//租用结束时间
			if( !"".equals(FID)) {
				newcmsrecord.set("ID",FID);//租户表id
				SDK.getBOAPI().update("BO_EU_SH_RCGY_ROOM_PSN", newcmsrecord);
					String sql1 = "select bindid from BO_EU_SH_RCGY_ROOM_PSN t where  t.ID = '"
							+ FID + "'";
					String bindid = CoreUtil.objToStr(DBSql.getString(sql1, "bindid"));// bindid
					SDK.getBOAPI().copyFileTo(ID, "ANNEX", FID, "BO_EU_SH_RCGY_ROOM_PSN",
							"ANNEX", bindid, null);
			}
			//发送企业微信消息
			if(!"".equals(APPLYUNIT) && !"".equals(APPLYNAMEID) ) {
				String sqluserId ="select EXT5  from ORGUSER  where USERID='"+APPLYNAMEID+"'";
		        String weixinid =CoreUtil.objToStr(DBSql.getString(sqluserId,"EXT5"));
		        String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+APPLYUNIT+"'";
		        List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
		        if (jbData.size() > 0 && !"".equals(weixinid)) {
		        	String content="您好！本单位为您已办理完成续租手续，如有疑问请联系本单位，经办人："+APPLYNAME+",电话："+APPLYTELEPHONE+"。";
		        	String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
					String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
					String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
					String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
					weComSendAnnouncementUtil.sendWeChatMessage(weixinid,"","text","",content,0,agentid,acessToken);
		        }
			}
		}
	}
}
